//
//  BDTishiViewController.m
//  BadBoy
//
//  Created by wen on 2017/4/27.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDTishiViewController.h"
#import <WebKit/WebKit.h>
#import "BDUserModel.h"

@interface BDTishiViewController ()<WKNavigationDelegate>

@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;

@end

@implementation BDTishiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.xieyiType == BDXieyiTypeUser) {
        self.title = ASLocalizedString(@"用户服务协议");
    }else if (self.xieyiType == BDXieyiTypePost) {
        self.title = ASLocalizedString(@"夜城社区管理规定(试行)");
    }
    [self requestDatasource];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.indicatorView startAnimating];
}

- (void)requestDatasource{
    NSString *url = [NSString stringWithFormat:@"%@tool/statisticsData", KBDBaseURL];
    NSString *headValid = [BDUserModel getHeaderValidation];
    RACSignal *signal = [[BDNetworkingServer yy_GET:url parameters:nil validation:headValid automaticProcessingError:YES] execute:nil];
    [signal subscribeNext:^(id  _Nullable x) {
        self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, kBDTopHeight, kBDScreenWidth, kBDScreenHeight - kBDTopHeight)];
        _webView.navigationDelegate = self;
        NSURLRequest *request = nil;
        if (self.xieyiType == BDXieyiTypeUser) {
            request = [NSURLRequest requestWithURL:[NSURL URLWithString:x[@"agreement_url"]]];
        }else if (self.xieyiType == BDXieyiTypePost) {
            request = [NSURLRequest requestWithURL:[NSURL URLWithString:x[@"management_url"]]];
        }
        
        [self.webView loadRequest:request];
        [self.view insertSubview:self.webView atIndex:0];
    }];
    
    [signal subscribeError:^(NSError * _Nullable error) {
        [self.indicatorView stopAnimating];
        [self showErrorViewWithTapBlock:^{
            
        }];
    }];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    [self.indicatorView stopAnimating];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    [self.indicatorView stopAnimating];
    [self showErrorViewWithTapBlock:^{
        
    }];
}

- (UIActivityIndicatorView *)indicatorView{
    if (!_indicatorView) {
        self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _indicatorView.hidesWhenStopped = YES;
        _indicatorView.frame = CGRectMake(0, kBDTopHeight, kBDScreenWidth, 50);
        [self.view addSubview:_indicatorView];
    }
    return _indicatorView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
